local skel = fk.CreateSkill {
  name = "emo__jiyu",
}

Fk:loadTranslationTable{
  ["emo__jiyu"] = "寄寓",
  [":emo__jiyu"] = "当其他角色获得你的牌后，若其中有红色牌，其可以令你回复一点体力；若其中有黑色牌，你可以对其使用一张【杀】，若其存活则你令其回复一点体力。",

  ["#emo__jiyu-recover"] = "寄寓：你可令%src回复一点体力",
  ["#emo__jiyu-slash"] = "寄寓：你可以对%src使用一张【杀】，再令其回复一点体力",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    local to
    local list = {}
    for _, move in ipairs(data) do
      if move.from == player and move.to and move.to ~= player and move.to:isAlive() and (move.to == to or to == nil) then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            local card = Fk:getCardById(info.cardId)
            if card.color ~= Card.NoColor then
              to = move.to
              table.insertIfNeed(list, card:getColorString())
            end
          end
        end
      end
    end
    if to and #list > 0 then
      event:setCostData(self, {to, list})
      return true
    end
  end,
  on_trigger = function (self, event, target, player, data)
    local to, list = table.unpack(event:getCostData(self))
    for _, n in ipairs(list) do
      if to.dead or not player:hasSkill(skel.name) then break end
      self:doCost(event, to, player, n)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if data == "red" then
      return player.room:askToSkillInvoke(target, { skill_name = skel.name, prompt = "#emo__jiyu-recover:"..player.id})
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data == "red" then
      room:recover { num = 1, skillName = skel.name, who = player , recoverBy = target}
    else
      local use = room:askToUseCard(player, {
        pattern = "slash", skill_name = skel.name, prompt = "#emo__jiyu-slash:"..target.id,
        extra_data = {exclusive_targets = {target.id}, bypass_distances = true}
      })
      if not use then return end
      room:useCard(use)
      if not target.dead then
        room:recover { num = 1, skillName = skel.name, who = target , recoverBy = player}
      end
    end
  end,
})

return skel
